<?php
/**
 * @title 295. 数据流的中位数 - 堆排序
 * @author start2004
 */

ini_set("memory_limit", "1024M");

// class Solution {
//
//     function find() {
//
//     }
// }

class MedianFinder {
    /**
     * initialize your data structure here.
     */
    function __construct() {
        $this->len = 0;

        /**
         * @since 2021-08-30 maxHeap->top <= minHeap->top
         */
        $this->minHeap = new SplMinHeap();
        $this->maxHeap = new SplMaxHeap();
    }

    /**
     * @param Integer $num
     * @return NULL
     */
    function addNum($num) {
        if($this->len == 0){
            $this->maxHeap->insert($num);
        } else {
            /**
             * @since 2021-08-30 插入最大堆
             */
            if($this->len % 2 == 0){
                if($num > $this->minHeap->top()){
                    $top = $this->minHeap->extract();
                    $this->minHeap->insert($num);
                    $this->maxHeap->insert($top);
                } else {
                    $this->maxHeap->insert($num);
                }
            } else {
                /**
                 * @since 2021-08-30 插入最小堆
                 */
                if($num < $this->maxHeap->top()){
                    $top = $this->maxHeap->extract();
                    $this->minHeap->insert($top);
                    $this->maxHeap->insert($num);
                } else {
                    $this->minHeap->insert($num);
                }
            }
        }

        /**
         * @since 2021-08-30 add数量
         */
        $this->len += 1;
    }

    /**
     * @return Float
     */
    function findMedian() {
        if($this->len % 2 == 1){
            return $this->maxHeap->top();
        } else {
            return ($this->maxHeap->top() + $this->minHeap->top()) / 2;
        }
    }
}

/**
 * @url http://127.0.0.1/leetcode/202108/2021.08.27_3.php
 */
$datas = [
    // [
    //     ["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"],
    //     [[],[1],[2],[],[3],[]]
    // ],
    //
    // [
    //     ["MedianFinder","addNum","addNum","addNum","addNum","addNum","findMedian"],
    //     [[],[-1],[-8],[-4],[-2],[-3],[]]
    // ],
    //
    // [
    //     ["MedianFinder","addNum","addNum","addNum","addNum","addNum","findMedian"],
    //     [[],[-1],[105],[102],[103],[101],[]]
    // ],
    //
    // [
    //     ["MedianFinder","addNum","addNum","addNum","addNum","addNum","findMedian"],
    //     [[],[-1],[5],[102],[103],[101],[]]
    // ],

    // [
    //     ["MedianFinder","addNum","addNum","addNum","addNum","findMedian"],
    //     [[],[-1],[5],[102],[103],[]]
    // ],
    //
    [
        ["MedianFinder","addNum","addNum","addNum","addNum","findMedian"],
        [[],[-1],[106],[102],[103],[]]
    ],

    [
        ["MedianFinder","addNum","addNum","addNum","findMedian"],
        [[],[-1],[102],[103],[]]
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
// foreach ($datas as $data){
//     if(count($data) <= 20){
//         var_dump($data);
//     } else {
//         var_dump(count($data));
//     }
//
//     $obj = new Solution();
//     $result = $obj->(...$data);
//     // $result = $obj->($xhprof->tree($data));
//     // $result = $obj->($xhprof->listNode($data));
//     // $result = $obj->find(...$data);
//     var_dump($result);
//     // if(count($result)<=20){
//     //     var_dump($result);
//     // } else {
//     //     var_dump(count($result));
//     // }
//     echo str_repeat(PHP_EOL, 3);
//     echo str_repeat("<br>", 3);
// }

foreach ($datas as $data){
    $obj = new $data[0][0](...$data[1][0]);

    for ($i=1; $i<count($data[0]); $i++){
        $func = $data[0][$i];
        $param = $data[1][$i];
        $result = $obj->$func(...$param);

        echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
        if($result === true){
            echo "True";
        } elseif($result === false) {
            echo "False";
        } elseif($result === null) {
            echo "Null";
        } elseif(is_array($result)) {
            var_dump($result);
        } else {
            echo $result;
        }
        echo PHP_EOL;
    }

    echo str_repeat(PHP_EOL, 3);
}
$xhprof->end();
